/****************************************************************************/
/*                                                                          */
/*   FILE:    physical.c	                                                */
/*   DATE:    31/07/97                                                      */
/*   PURPOSE: DVB-CI Physical layer - entry point into PHYSICAL task        */
/*			- Initialization Module				    					    */
/*			- Main Module					    							*/
/*		    - Ending Module												    */
/*   TAB STOPS : 4															*/
/*   HISTORY:				                                    			*/
/*   Version	Date	  	    Written by	     Notes	    	   			*/
/*	 1.0	    31/07/1997      SCM              Initial implementation 	*/
/*	 1.04		19/11/1997		SCM         	 Add phmsg_header_t structure	*/
/*						in PHYSICAL_State										*/
/*                                                                    	    */
/*--------------------------------------------------------------------------*/
/*   GENERAL COMMENTS                                                 	    */
/*																		    */
/****************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include "sm_trace.h"
#include "itc.h"
#include "physical.h"
#include "phydrv.h"
#include "camusb.h"
#include "sm_kapi.h"


/*--------------------------------------------------------------------------*/
/* Global Variables		                                            		*/
/*--------------------------------------------------------------------------*/
physlot_t	g_phy_tab_slot[MAXSLOTID];	// slot table
unsigned char*	s_phy_hwbuf;

int		g_pipePhy = -1;

sm_sem_t s_phy_sem;
static unsigned char s_u8Inited = 0;
pfPHYSICALEventCallback pfPhysicalStatusCallback = NULL;

int PHYSICAL_Event(PHY_STATUS_T eStatus)
{
	if(pfPhysicalStatusCallback)
		pfPhysicalStatusCallback(eStatus);

	return 0;
}

/*--------------------------------------------------------------------------*/
/* External Variables		                                           		*/
/*--------------------------------------------------------------------------*/

/* ======================================================================== */
/* NAME : physical							    							*/
/* 									    									*/
/* DESCRIPTION : entry point into PHYSICAL task :				    		*/
/*		 - Initialization Module				   							*/
/*		 - Main Module						    							*/
/*		 - Ending Module				         							*/
/*	The Main Module is an infinite loop :								    */
/*		 - waits for events (messages from PHYSICAL task or signals from PHYSICAL*/
/*		   driver)															*/
/*		 - reads the first received event  			   						*/
/*		 - calls the PHYSICAL State Diagram PHYSICAL_State	  	        	*/
/*	                                                                  		*/
/* 									 										*/
/* INPUT PARAMETERS :							    						*/
/*									    									*/
/* OUTPUT PARAMETERS :							  							*/
/*									   										*/
/* RETURN CODE								   								*/
/*	 0    	 	success														*/
/*  -1			error														*/
/*									   										*/
/* ======================================================================== */
int physical(void* param)
{
	int			rc = 0;
	xmsg_t		xmsg;

	SM_TRACE_ENTRY(TRMOD_PHY, "physical() - entry\r\n");

	if (!PHYSICAL_Init())
	{
		SM_TRACE_ERROR(TRMOD_PHY, "physical() - physical init failed\r\n");
		rc = -1;
	}

	while(rc == 0)
	{
		s_u8Inited = 1;
		SM_TRACE_INFO(TRMOD_PHY, "physical() - wait for event in %d ms...\r\n", PHPOLL_TIMEOUT);
		if (!ITC_RecvXmsg(&xmsg,ITC_INFINITE))//500
		{
			SM_TRACE_ERROR(TRMOD_PHY, "physical() - Wait xmsg Error in return code\r\n");
			rc = -1;
			break;
		}

		if (xmsg.m_type == XMG_EXIT)
		{
			SM_TRACE_CRITICAL(TRMOD_PHY, "physical() - receive XMG_EXIT\r\n");
			break;
		}

		if (xmsg.m_type)  //Process the received xmsg
		{
			SM_OsSemWait(&s_phy_sem, -1);
			PHYSICAL_State(&xmsg); //Call the phy State Diagram
			SM_OsSemRelease(&s_phy_sem);
		}
	}

	PHYSICAL_End();		//Call the Ending Module

	SM_TRACE_CRITICAL(TRMOD_PHY, "physical() - return rc=%d\r\n", rc);
	return rc;
}


int physical_read(void* param)
{
	int			rc = 0;
	xmsg_t		xmsg;
	int			dataReading = 0;

	SM_TRACE_ENTRY(TRMOD_PHY, "physical_read() - entry\r\n");

	SM_TRACE_INFO(TRMOD_PHY, "physical_read() - start wait\r\n");
	while(s_u8Inited == 0)
	{
		SM_Sleep(50);
		SM_TRACE_INFO(TRMOD_PHY, "physical_read() - waiting ....\r\n");
	}
	SM_TRACE_INFO(TRMOD_PHY, "physical_read() - waiting OK \r\n");
	while(rc == 0)
	{
		xmsg.m_bd.m_ph_data.m_pbData = s_phy_hwbuf;
		SM_TRACE_INFO(TRMOD_PHY, "physical_read() - read phy driver...\r\n");
		if (!PHYS_DRVRead(&xmsg)) //Poll the PHYSICAL Driver
		{
			SM_TRACE_ERROR(TRMOD_PHY, "physical_read() - Error in received event\r\n");
			rc = -1;
			break;
		}

		if(xmsg.m_type == PH_DATA && dataReading != 1)
		{
			dataReading = 1;
		}
		else
		{
			if(xmsg.m_type == PH_ERROR && dataReading == 1)
			{
				PHYSICAL_Event(PHY_STATUS_FAILED);	
			}
			dataReading = 0;
		}

		if (xmsg.m_type)
		{
			SM_OsSemWait(&s_phy_sem, -1);
			PHYSICAL_State(&xmsg); //Process the received SIGNAL
			SM_OsSemRelease(&s_phy_sem);
		}
	}

	PHYSICAL_End();		//Call the Ending Module

	SM_TRACE_CRITICAL(TRMOD_PHY, "physical_read() - return rc=%d\r\n", rc);
	return rc;
}



/* ======================================================================== */
/* NAME : PHYSICAL_Init							    						*/
/* 									    									*/
/* DESCRIPTION : Initialization Module					    				*/
/*		 physical variables initialization			    					*/
/*		 physical State Diagram initialization 	            				*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*									    									*/
/* OUTPUT PARAMETERS :							    						*/
/*									    									*/
/* RETURN CODE :							    							*/
/*	1   	 	successful initialization			   						*/
/*  0			error while initialization			    					*/
/*									   										*/
/* ======================================================================== */
BOOL PHYSICAL_Init(void)
{
	int i;

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_Init() - entry\r\n");

	if (!ITC_Init(PHYSICAL))
	{
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_Init() - itc init failed\r\n");
		return FALSE;
	}

	/* PHYSICAL Driver Initialization */
	if (PHYS_DRVOpen() <= 0)
	{
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_Init() - phys drvopen failed\r\n");
		return FALSE;
	}

	for (i=0; i < MAXSLOTID; i++)
	{
		g_phy_tab_slot[i].m_state = PHY_NOT_USED;
		g_phy_tab_slot[i].m_slotId  = 0;
	}

	s_phy_hwbuf = ITC_DynAlloc(MAX_LPDU_SIZE);
	if (s_phy_hwbuf == NULL)
	{
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_Init() - alloc mem(%d) failed\r\n", MAX_LPDU_SIZE);
		return -1;
	}

	SM_CreateSem(&s_phy_sem, NULL, 1);

	SM_TRACE_INFO(TRMOD_PHY, "start_alltask() - create physical task...\r\n");
	

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_Init() - return true\r\n");
	return TRUE;
}
/* End of Initialization Module */
/* ============================ */


/* ======================================================================== */
/* NAME : PHYSICAL_End							    						*/
/* 									    									*/
/* DESCRIPTION : Ending Module							    				*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*									    									*/
/* OUTPUT PARAMETERS :							    						*/
/*									    									*/
/* RETURN CODE :							    							*/
/*									   										*/
/* ======================================================================== */
void PHYSICAL_End(void)
{
	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_End() - entry\r\n");

	if (s_phy_hwbuf)
	{
		ITC_DynFree(s_phy_hwbuf);
		s_phy_hwbuf = NULL;
	}

	PHYS_DRVClose();

	ITC_End();
/*
	if (g_pipePhy >= 0)
	{
//		if (close(g_pipePhy))
		{
			SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_End() - close g_pipePhy=%d failed, %s\r\n", 
					g_pipePhy, strerror(errno));
		}
		g_pipePhy = -1;
	}
*/	
	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_End() - return\r\n");
}
/* End of Initialization Module */
/* ============================ */


/* ======================================================================== */
/* NAME : PHYSICAL_State													*/
/* 									    									*/
/* DESCRIPTION : Entry point of the physical State Diagram Module			*/
/*		Read the event					       							    */
/*		Read the message if the event = xmsg		    				    */
/*		query the PHYSICAL driver if the event = SIGNAL						*/
/*		Find the current slotId and the matching state diagram index 		*/
/*		Call the current transition matches the received event	   			*/
/* 									   										*/
/* INPUT PARAMETERS :							   							*/
/*	g_phy_tab_slot[i].m_state gives the current state of the State Diagram  		*/
/*	for the current physical slot (index i)					    			*/
/*									   										*/
/* OUTPUT PARAMETERS :							    						*/
/*	g_phy_tab_slot[i].m_state is updated					    					*/
/*									  										*/
/* RETURN CODE :							  							    */
/*									 									    */
/* ======================================================================== */
void PHYSICAL_State(xmsg_t*	pXmsg)
{
	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_State() - entry,pXmsg=0x%08X\r\n", pXmsg);

	SM_TRACE_INFO(TRMOD_PHY, "PHYSICAL_State() - received %s(0x%02X)\r\n",
					XMSG_NAME(pXmsg->m_type), pXmsg->m_type);

	switch (pXmsg->m_type)
	{
	case LPDU :			/* Physical <-- Link */
		SM_TRACE_INFO(TRMOD_PHY, "PHYSICAL_State() - received LPDU\r\n");
		PHYSICAL_Lpdu(pXmsg);
	    break;

	case PH_DATA :			/* Physical <-- Driver */
		SM_TRACE_INFO(TRMOD_PHY, "PHYSICAL_State() - received PH_DATA\r\n");
		PHYSICAL_Data(pXmsg);
		break;

	case PH_ERROR :			/* Physical <-- Driver */
		SM_TRACE_INFO(TRMOD_PHY, "PHYSICAL_State() - received PH_ERROR\r\n");
		PHYSICAL_Error(pXmsg);
		break;

	case PH_INSERT :		/* Physical <-- Driver */
		SM_TRACE_INFO(TRMOD_PHY, "PHYSICAL_State() - received PH_INSERT\r\n");
		PHYSICAL_Insert(pXmsg);
		break;

	case PH_EXTRACT :		/* Physical <-- Driver */
		SM_TRACE_INFO(TRMOD_PHY, "PHYSICAL_State() - received PH_EXTRACT\r\n");
		PHYSICAL_Extract(pXmsg);
		break;

	default :			/* unknown message */
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_State() - Unknown message 0x%02X\r\n", pXmsg->m_type);
		break;
	} //end switch

	FreeXmsg(pXmsg);

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_State() - return\r\n");
}
/* End of physical State Diagram */
/* ============================= */


/* ======================================================================== */
/* NAME : PHYSICAL_Lpdu								    					*/
/* 									    									*/
/* DESCRIPTION : Physical State diagram : LPDU received 					*/
/*		- Decod the LPDU													*/
/*		- Find the physical slot											*/
/*		- Call the action													*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*		pXmsg->m_bd.m_lpdu contains the LPDU message received from LINK Task		*/
/*									    									*/
/* OUTPUT PARAMETERS :							    						*/
/*									    									*/
/* RETURN CODE :							    							*/
/*		0 : the calling function must free the memory containing the message*/
/*		1 : the calling function do nothing 								*/
/*									   										*/
/* ======================================================================== */
int PHYSICAL_Lpdu(xmsg_t* pXmsg)
{
	int 		index;		/* index of g_phy_tab_slot[] for the current module connection  	*/

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_Lpdu() - entry,pXmsg=0x%08X\r\n", pXmsg);

	index = PHYSICAL_GetSlot(pXmsg->m_bd.m_lpdu.m_slotId);
	if (index >= 0)
	{
		PHYSICAL_Action(index, pXmsg);
	}

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_Lpdu() - return 0\r\n");
	return 0;
}


/* ======================================================================== */
/* NAME : PHYSICAL_Insert							    					*/
/* 									    									*/
/* DESCRIPTION : Physical State diagram : Module Insertion Event received 	*/
/*		- Find the physical slot											*/
/*		- Initialize the link between the module and the host				*/
/*		- Call the action													*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*									    									*/
/* OUTPUT PARAMETERS :							    						*/
/*									    									*/
/* RETURN CODE :							    							*/
/*		0 : the calling function must free the memory containing the message*/
/*		1 : the calling function does nothing 								*/
/*									   										*/
/* ======================================================================== */
int PHYSICAL_Insert(xmsg_t* pXmsg)
{
	int 			index;		/* index of g_phy_tab_slot[] for the current module connection	*/
	unsigned short 	slotId;		/* current slot identifier						*/

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_Insert() - entry, pXmsg=0x%08X\r\n", pXmsg);

	slotId = pXmsg->m_bd.m_ph_insert.m_slotId;

	/* Add a new instance of the Physical State Diagram for the new module					 			*/
	index = PHYSICAL_GetIndex(slotId);		/* get the new instance for the physical state diagram 		*/
	if (index < 0)
	{
		/* Send IHM_WARNING to IHM Task */
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_Insert() - can't get new instance or module already connected\r\n");
		return 1;	/* can't get new instance or module already connected, the request is ignored ; no memory management */
    }

	SM_TRACE_INFO(TRMOD_PHY, "PHYSICAL_Insert() - alloc new slot_index=%d\r\n", index);

	/* Call the physical state diagram */
	if (!PHYSICAL_Action(index, pXmsg))
	{
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_Insert() - physical action failed\r\n");
		PHYSICAL_FreeIndex(index, slotId);		/* error : free the new instance */
	}
    else
    {	/* no DVB module, warning 		*/

		/* Send IHM_WARNING to IHM Task */
    }

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_Insert() - return\r\n");
	return 1;		/* no memory management */
}


/* ======================================================================== */
/* NAME : PHYSICAL_Error							    					*/
/* 									    									*/
/* DESCRIPTION : Physical State diagram : Module Error Event received 		*/
/*		- Find the physical slot											*/
/*		- Call the action													*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*									    									*/
/* OUTPUT PARAMETERS :							    						*/
/*									    									*/
/* RETURN CODE :							    							*/
/*		0 : the calling function must free the memory containing the message*/
/*		1 : the calling function does nothing 								*/
/*									   										*/
/* ======================================================================== */
int PHYSICAL_Error(xmsg_t* pXmsg)
{
	int 			index;		/* index of g_phy_tab_slot[] for the current module connection	*/
	unsigned short 	slotId;		/* current slot identifier						*/

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_Error() - entry,pXmsg=0x%08X\r\n", pXmsg);

	slotId = pXmsg->m_bd.m_ph_error.m_slotId; //modify by pmy

	/* Add a new instance of the Physical State Diagram for the new module					 			*/
	index = PHYSICAL_GetIndex(slotId);		/* get the new instance for the physical state diagram 		*/
	if (index < 0)
	{
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_Error() - invalid slotId=%d\r\n", slotId);
		/* Send IHM_WARNING to IHM Task */
		return 1;	/* can't get new instance or module already connected, the request is ignored ; no memory management */
    }

	/* Call the physical state diagram */
	PHYSICAL_Action(index, pXmsg);

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_Error() - return\r\n");
	return 1;		/* no memory management */
}


/* ======================================================================== */
/* NAME : PHYSICAL_Data							    						*/
/* 									    									*/
/* DESCRIPTION : Physical State diagram : received data from Physical Driver*/
/*		- Find the physical slot											*/
/*		- Call the action													*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*									    									*/
/* OUTPUT PARAMETERS :							    						*/
/*									    									*/
/* RETURN CODE :							    							*/
/*		0 : the calling function must free the memory containing the message*/
/*		1 : the calling function do nothing 								*/
/*									   										*/
/* ======================================================================== */
int PHYSICAL_Data(xmsg_t* pXmsg)
{
	int 			index;		/* index of g_phy_tab_slot[] for the current module connection	*/
	unsigned short 	slotId;	/* current slot identifier						*/

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_Data() - entry,pXmsg=0x%08X\r\n", pXmsg);

	slotId = pXmsg->m_bd.m_ph_data.m_slotId;

	index = PHYSICAL_GetSlot(slotId);
	if (index < 0)
	{
		/* Send IHM_WARNING to IHM Task */
		return 1;	/* can't get new instance or module already connected, the request is ignored ; no memory management */
    }

	/* Call the physical state diagram */
	PHYSICAL_Action(index, pXmsg);

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_Data() - return\r\n");
	return 1;		/* no memory management */
}


/* ======================================================================== */
/* NAME : PHYSICAL_Extract							    					*/
/* 									    									*/
/* DESCRIPTION : Physical State diagram : Module Extraction Event received 	*/
/*		- Find the physical slot											*/
/*		- Call the action													*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*									    									*/
/* OUTPUT PARAMETERS :							    						*/
/*									    									*/
/* RETURN CODE :							    							*/
/*		0 : the calling function must free the memory containing the message*/
/*		1 : the calling function does nothing 								*/
/*									   										*/
/* ======================================================================== */
int PHYSICAL_Extract(xmsg_t* pXmsg)
{
	int				index;		/* index of g_phy_tab_slot[] for the current module connection	*/
	unsigned short	slotId;		/* current slot identifier						*/

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_Extract() - entry,pXmsg=0x%08X\r\n",pXmsg);

	slotId = pXmsg->m_bd.m_ph_extract.m_slotId;

	index = PHYSICAL_GetSlot(slotId);
	if (index < 0)
	{
		/* Send IHM_WARNING to IHM Task */
		return 1;	/* can't find the slot identifier, no memory management */
    }

	/* Call the physical state diagram */
	PHYSICAL_Action(index, pXmsg);

	PHYSICAL_FreeIndex(index, slotId);		/* free the new instance */

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_Extract() - return 1\r\n");
	return 1;		/* no memory management */
}


/* ======================================================================== */
/* NAME : PHYSICAL_GetIndex					 								*/
/* 									    									*/
/* DESCRIPTION : Get a new entry in g_phy_tab_slot								*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*		slot_in : physical slot identifier									*/
/*									 										*/
/* OUTPUT PARAMETERS :							   							*/
/*		g_phy_tab_slot is updated												*/
/*									    									*/
/* RETURN CODE								    							*/
/*		 -1 : no more entry													*/
/*		 -2 : already connected												*/
/*		>=0 : index in g_phy_tab_slot											*/
/*									  										*/
/* ======================================================================== */
int PHYSICAL_GetIndex(unsigned short slotId)
{
int index;				/* return code : entry found */

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_GetIndex() - entry,slotId=%d\r\n",slotId);

	for (index=0; index < MAXSLOTID; index++)
	{
		if (g_phy_tab_slot[index].m_state == PHY_NOT_USED)   /* take the first free entry, reserve it */
		{
			g_phy_tab_slot[index].m_slotId	= slotId;
			g_phy_tab_slot[index].m_state   = PHY_IDLE;
			SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_GetIndex() - alloc success,slotId %d,index=%d\r\n",
							slotId, index);
			return index;
		}
		else
		{
			if (g_phy_tab_slot[index].m_slotId == slotId)
			{
				SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_GetIndex() - slot already conneted,slotId %d,index=%d\r\n",
						slotId, index);
				return -2;		/* slot already connected */
			}
		}
    }

	SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL::PHYSICAL_GetIndex : no more entry\r\n");

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_GetIndex() - return\r\n");
	return -1;				/* no more entry */
}


/* ======================================================================== */
/* NAME : PHYSICAL_GetSlot					 								*/
/* 									    									*/
/* DESCRIPTION : Get the current entry in g_phy_tab_slot						*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*		slotId : slot identifier											*/
/*									 										*/
/* OUTPUT PARAMETERS :							   							*/
/*									    									*/
/* RETURN CODE								    							*/
/*		 -1 : entry not found												*/
/*		>=0 : index in g_phy_tab_slot											*/
/*									  										*/
/* ======================================================================== */
int PHYSICAL_GetSlot(unsigned short slotId)
{
	int index;				/* return code : entry found */

	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_GetSlot() - entry\r\n");

	for (index=0; index < MAXSLOTID; index++)
	{
		if ((g_phy_tab_slot[index].m_state != PHY_NOT_USED) && (g_phy_tab_slot[index].m_slotId == slotId))
			return index;	/* entry found 	   */
	}

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_GetSlot() - return\r\n");
	return -1;				/* entry not found */
}


/* ======================================================================== */
/* NAME : PHYSICAL_FreeIndex				 								*/
/* 									    									*/
/* DESCRIPTION :Delete an entry in g_phy_tab_slot								*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*		index : index of the entry to delete								*/
/*		slotId  : slot identifier												*/
/*									 										*/
/* OUTPUT PARAMETERS :							   							*/
/*									    									*/
/* RETURN CODE								    							*/
/*		< 0 : entry not found												*/
/*		>=0 : entry deleted													*/
/*									  										*/
/* ======================================================================== */
int PHYSICAL_FreeIndex(int index, unsigned short slotId)
{
	SM_TRACE_ENTRY(TRMOD_PHY, "PHYSICAL_FreeIndex() - entry,index=%d,slotId=%d\r\n",
					index, slotId);

    if ((index < 0) && (index >= MAXSLOTID))
	{
		SM_TRACE_ERROR(TRMOD_PHY, "PHYSICAL_FreeIndex() - invalid index=%d\r\n", index);
    	return -1;
	}

	g_phy_tab_slot[index].m_state = PHY_NOT_USED;				/* free the entry 	*/
	g_phy_tab_slot[index].m_slotId  = 0;

	SM_TRACE_RETURN(TRMOD_PHY, "PHYSICAL_FreeIndex() - return\r\n");
	return index;
}


int PHYSICAL_RegistorErrorCallback(pfPHYSICALEventCallback cb)
{
	pfPhysicalStatusCallback = cb;
	return 0;
}


